<?php
/**
 * User:chendenan
 * Created On 2022/3/19 0019 21:41
 */
namespace app\common\business;
use app\common\model\mysql\GoodsSku as GoodsSkuModel;
class GoodsSku extends BusBase{
    public $model = null;
    public function __construct(){
        $this->model = new GoodsSkuModel();
    }
    public function save($data){
        if(!$data['stock'] || !$data['price'] || !$data['cost_price']){
            return false;
        }
        try{
            $result = $this->model->create($data);
            return $result;
        }catch(\Exception $e){
            trace($e->getMessage(),"error");
            return false;
        }
        return true;
    }

    /**
     * 批量新增
     * @param $data
     */
    public function saveAll($data){
        if(!$data['skus']){
            return false;
        }
        foreach ($data['skus'] as $value){
            $insertData[] = [
                "goods_id" => $data['goods_id'],
                "specs_value_ids" => $value['propvalnames']['propvalids'],
                "price" => $value['propvalnames']['skuSellPrice'],
                "cost_price" => $value['propvalnames']['skuMarketPrice'],
                "stock" => $value['propvalnames']['skuStock'],
            ];
        }
        try {
            $result = $this->model->saveAll($insertData);
            return $result->toArray();
        }catch (\Exception $e){
            trace($e->getMessage(),"error");
            return false;
        }
        return true;
    }

    /**
     * 查询SKU信息和关联的商品信息
     * @param $id
     * @return array
     */
    public function getSkuAndGoods($id){
        try {
            $result = $this->model->with("goods")->find($id);
        }catch(\Exception $e){
            trace($e->getMessage(),"error");
            return [];
        }

        if(!$result){
            return [];
        }
        $result = $result->toArray();
        if($result['status']!=config("status.mysql.table_normal")){
            return [];
        }
        return $result;
    }

    public function getSkusByGoodsId($goodsId=0){
        if(!$goodsId){
            return [];
        }
        try {
            $skus = $this->model->getByGoodsId($goodsId);
        }catch(\Exception $e){
            trace($e->getMessage(),"error");
            return [];
        }
        return $skus->toArray();
    }
    public function getInIds($ids){
        try{
            $result = $this->model->getInIds($ids);
        }catch (\Exception $e){
            trace($e->getMessage(),"error");
            return [];
        }
        return $result->toArray();
    }

    /**
     * 更新库存
     * @param $data
     * @return bool
     */
    public function updateStock($data){
        foreach ($data as $value) {
            $this->model->decStock($value['id'],$value['num']);
        }
        return true;
    }
    /**
     * 批量更新库存
     * @param $data
     * @return bool
     */
    public function updateAllStock($data){
        if(!is_array($data)){
            return false;
        }
        try {
            $this->model->saveAll($data);
            return true;
        } catch (\Exception $e) {
            trace($e->getMessage(),"error");
            return false;
        }
    }
}